package users

import (
	"net/http"
	"product/common/enumeration"
	"product/common/utils"
	"product/common/validator"

	"github.com/zeromicro/go-zero/rest/httpx"
	"product/authorization/serve/internal/logic/users"
	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"
)

// RefreshTokenHandler refresh token
func RefreshTokenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.RefreshTokenReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := users.NewRefreshTokenLogic(r.Context(), svcCtx)
		resp, err := l.RefreshToken(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		}
		if validator.IsEmptyString(resp.Token) {
			httpx.OkJsonCtx(r.Context(), w, utils.Fail("刷新token失败"))
			return
		}
		httpx.OkJsonCtx(r.Context(), w, utils.Success(resp, "刷新成功", enumeration.ProcessingSuccess))
	}
}
